package com.example.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.Util.ZipUtils;
import com.example.entity.Admin;
import com.example.entity.PushMessage;
import com.example.entity.UploadFile;
import com.example.entity.User;
import com.example.service.AdminServiceImpl;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.UUID;

/**
 * @author 叶凯乐
 */
@Controller
@CrossOrigin(allowCredentials = "true", allowedHeaders = "*")
public class AdminController {
    private static final String PATH_IMG = "C:\\img\\";
    private static final String PATH_ZIP = "C:\\zips\\";
    @Resource
    private AdminServiceImpl adminService;

    @PostMapping("/adminLogin")
    @ResponseBody
    public boolean doLogin(String adminId, String password) {
        return adminService.login(adminId, password);
    }
    @PostMapping("/watchChargeUserMessage")
    @ResponseBody
    public ArrayList<User>watchUserMessage(String adminId){
        return adminService.allAdminChargeUserMessage(adminId);
    }


    @PostMapping("/adminRegister")
    @ResponseBody
    public boolean doRegister(Admin admin) {
        return adminService.register(admin);
    }

    @PostMapping("/listUserId")
    @ResponseBody
    public ArrayList<UploadFile> list(String adminId) {
        return adminService.queryByAdminId(adminId);
    }

    @PostMapping("/download")
    @ResponseBody
    public void down(HttpServletResponse response,String fileJson) throws Exception {
        ArrayList<StringBuilder> fileList = new ArrayList<>();
        JSONObject jsonObject = JSONObject.parseObject(fileJson);
        String key = "file";
        JSONArray jsonArray = jsonObject.getJSONArray("file");
        for (int i = 0; i < jsonArray.size(); i++) {
            fileList.add(new StringBuilder().append(PATH_IMG).append(JSON.parseObject(jsonArray.getString(i)).getString("fileName")));
        }
        String []files=new String[fileList.size()];
        String fileName= PATH_ZIP+UUID.randomUUID()+".zip";
        ZipUtils.toZip(files,fileName,false);
        FileInputStream fis=new FileInputStream(new File(fileName));
        ServletOutputStream out=response.getOutputStream();
        byte []bytes=new byte[1024*1024];
        int temp=0;
        while ((temp=fis.read(bytes))!=-1){
            out.write(bytes,0,temp);
         }
        fis.close();
        out.close();
    }

    @PostMapping("/listContent")
    @ResponseBody
    public ArrayList<PushMessage> listContent(String adminId) {
        return adminService.showContent(adminId);
    }
}
